package xyz.hubery.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import xyz.hubery.mapper.CategoryMapper;
import xyz.hubery.pojo.Category;
import xyz.hubery.service.CategoryService;

import java.util.List;

@DubboService
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        refreshRedisCategory();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        if (category != null) {
            category.setStatus(status);
            categoryMapper.updateById(category);
            refreshRedisCategory();
        }
    }

    @Override
    public void delete(Long[] ids) {
        categoryMapper.deleteBatchIds(List.of(ids));
        refreshRedisCategory();
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Page<Category> search(int page, int size) {
        return categoryMapper.selectPage(Page.of(page, size), null);
    }

    @Override
    public List<Category> findAll() {

        // 查询缓存
        ListOperations listOperations = redisTemplate.opsForList();
        List<Category> categories = listOperations.range("categories", 0, -1);
        if (categories != null && !categories.isEmpty()) {
            System.out.println("从redis中查询广告");
            return categories;
        }
        System.out.println("从mysql中查询广告");
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getStatus, 1));
        listOperations.leftPushAll("categories", categoryList); // 添加缓存
        return categoryList;
    }

    /**
     * 更新redis中的广告数据
     */
    public void refreshRedisCategory() {
        // 从数据库查询广告
        QueryWrapper<Category> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        // 删除redis中的原有广告数据
        redisTemplate.delete("categories");
        // 将新的广告数据同步到redis中
        ListOperations<String, Category> listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("categories", categories);
    }
}
